package com.css.fxfzypg.modules.preAssessResult.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.SettingSeismic.vo.SettingSeismicDetailsVO;
import com.css.fxfzypg.modules.baseData.entity.BuildingVulnerability;
import com.css.fxfzypg.modules.baseData.queryParams.BuildingVulnerabilityQueryParams;
import com.css.fxfzypg.modules.hypergraph.service.HypergraphService;
import com.css.fxfzypg.modules.preAssessResult.entity.SeiCapDistribution;
import com.css.fxfzypg.modules.preAssessResult.entity.Vulnerability;
import com.css.fxfzypg.modules.preAssessResult.service.PreAssessResultService;
import com.css.fxfzypg.modules.preAssessResult.vo.DistrictVO;
import com.css.fxfzypg.modules.preAssessResult.vo.MatrixVo;
import com.css.fxfzypg.modules.preAssessResult.vo.PreAssessTaskInfoVo;
import com.css.fxfzypg.modules.surveytaskmanage.vo.TaskVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author xzy
 * @date 2022/10/13 16:57
 * @description
 **/
@RestController
@RequestMapping("/preAssessResult")
@Slf4j
public class PreAssessResultController {
    @Autowired
    PreAssessResultService preAssessResultService;
    @Resource
    HypergraphService hypergraphService;

    /**
     * 预评估任务列表
     * @param taskVo,request
     * @return
     */
    @SysLog("预评估任务列表查询")
    @GetMapping("/taskPage")
    public RestResponse taskPage(TaskVo taskVo, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=preAssessResultService.taskPage(taskVo,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 待分配设定地震任务列表
     * @param taskVo,request
     * @return
     */
    @SysLog("待分配设定地震任务列表查询")
    @GetMapping("/undistributedTaskPage")
    public RestResponse undistributedTaskPage(TaskVo taskVo, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=preAssessResultService.undistributedTaskPage(taskVo,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取预评估任务详情
     * @param taskId
     * @return
     */
    @GetMapping("/getPreAssessTaskInfo")
    public RestResponse getPreAssessTaskInfo(String taskId){
        RestResponse restResponse=null;
        try{
            PreAssessTaskInfoVo preAssessTaskInfoVo=preAssessResultService.getPreAssessTaskInfo(taskId);
            restResponse= RestResponse.succeed(preAssessTaskInfoVo);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 判断是否有设定地震
     * @param taskId
     * @return
     */
    @GetMapping("/isHaveSetEarthquake")
    public RestResponse isHaveSetEarthquake(String taskId){
        RestResponse restResponse=null;
        try{
            Boolean flag=preAssessResultService.isHaveSetEarthquake(taskId);
            restResponse= RestResponse.succeed(flag);
        }catch (Exception e){
            String errormessage = e.getMessage();
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 设定地震统计
     * @param taskId
     * @return
     */
    @GetMapping("/setEarthquakeCensus")
    public RestResponse setEarthquakeCensus(String taskId){
        RestResponse restResponse=null;
        try{
            List<SettingSeismicDetailsVO> list=preAssessResultService.setEarthquakeCensus(taskId);
            restResponse= RestResponse.succeed(list);
        }catch (Exception e){
            String errormessage="查询成功";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 更新设定地震序号
     * @param parMap
     * @return
     */
    @PostMapping("/updateEarthquakeCensus")
    public RestResponse updateEarthquakeCensus(@RequestBody Map<String,String> parMap){
        RestResponse restResponse=null;
        try{
            int i =preAssessResultService.updateEarthquakeCensus(parMap.get("taskId"));
            restResponse= RestResponse.succeed("更新成功");
        }catch (Exception e){
            String errormessage="更新失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 设定地震删除，批量删除
     * @param faultNum
     * @return
     */
    @PostMapping("/setEarthquakeDelete")
    public RestResponse setEarthquakeDelete(@RequestParam("faultNum") String faultNum,@RequestParam("taskId") String taskId){
        RestResponse restResponse=null;
        try{
            preAssessResultService.setEarthquakeDelete(faultNum,taskId);
            restResponse= RestResponse.succeed("删除成功");
        }catch (Exception e){
            String errormessage="删除失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 将父任务的设定地震按震中位置所在省分配给各子任务
     * @param taskId
     * @return
     */
    @GetMapping("/assignment")
    public RestResponse assignment(String taskId){
        RestResponse restResponse=null;
        try{
            preAssessResultService.assignment(taskId);
            //刷新指定超图工作空间数据源数据集
            boolean bool = hypergraphService.getWorkspaceReload("setting_seismic_point");

            restResponse= RestResponse.succeed("分配成功");
        }catch (Exception e){
            String errormessage="分配失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 设定地震明細列表查询
     * @param settingSeismicDetailsVO,request
     * @return
     */
    @GetMapping("/getDetailPage")
    public RestResponse getDetailPage(SettingSeismicDetailsVO settingSeismicDetailsVO, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=preAssessResultService.getDetailPage(settingSeismicDetailsVO,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 设定地震明細列表查询
     * @param settingSeismicDetailsVO,request
     * @return
     */
    @GetMapping("/getDetailsPage")
    public RestResponse getDetailsPage(SettingSeismicDetailsVO settingSeismicDetailsVO, HttpServletRequest request){
        RestResponse response = null;
        try{
            if (PlatformObjectUtils.isEmpty(settingSeismicDetailsVO.getSsId())){
                String errorMessage = "参数为null";
                response = RestResponse.fail(errorMessage);
            }else {
                int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
                int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
                Map<String,Object> page=preAssessResultService.getDetailsPage(settingSeismicDetailsVO,curPage,pageSize);
                response = RestResponse.succeed(page);
            }
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /**
     * 设定地震明細列删除，批量删除
     * @param ids
     * @return
     */
    @PostMapping("/detailDelete/{ids}")
    public RestResponse detailDelete(@PathVariable("ids") String ids){
        RestResponse restResponse=null;
        try{
            preAssessResultService.detailDelete(ids);
            restResponse= RestResponse.succeed("删除成功");
        }catch (Exception e){
            String errormessage="删除失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 生成建筑物抗震能力分布
     * @param taskId
     * @param type
     * 0:区县，1:乡镇，2:格网
     * @return
     */
    @PostMapping("/seiCapCalculation")
    public RestResponse seiCapCalculation(@RequestParam("taskId") String taskId,@RequestParam("type") String type){
        RestResponse restResponse=null;
        try{
            restResponse= preAssessResultService.seiCapCalculation(taskId,type);
            //刷新指定超图工作空间数据源数据集
            boolean bool = hypergraphService.getWorkspaceReload("seismic_capacity_distribution");

        }catch (Exception e){
            String errormessage="建筑物抗震能力分布生成失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 安保系统调用   生成建筑物抗震能力分布
     * @param
     * @param
     *
     * @return
     */
    @PostMapping("/seiCapCalculationToAb")
    public RestResponse seiCapCalculationToAb(@RequestBody DistrictVO districtVOS){
        RestResponse restResponse=null;
        try{
            restResponse= preAssessResultService.seiCapCalculationToAb(districtVOS);

        }catch (Exception e){
            String errormessage="建筑物抗震能力分布生成失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查看建筑物抗震能力分布
     * @param taskId
     * @return
     */
    @GetMapping("/getSeiCapCalculation")
    public RestResponse getSeiCapCalculation(String taskId,String dzIntensity,String type){
        // public RestResponse getSeiCapCalculation(String taskId,String dzIntensity){
        RestResponse restResponse=null;
        try{
            List<SeiCapDistribution> list=preAssessResultService.getSeiCapCalculation(taskId,dzIntensity,type);
            // List<SeiCapDistribution> list=preAssessResultService.getSeiCapCalculation(taskId,dzIntensity);
            restResponse= RestResponse.succeed(list);
        }catch (Exception e){
            String errormessage="查看失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(503,errormessage);
        }
        return restResponse;
    }

    /**
     * 导出抗震能力分布
     */
    @GetMapping("/exportSeiCapCalculation")
    public void exportSeiCapCalculation( String taskId,String type, HttpServletResponse resp){
        try{
            preAssessResultService.exportSeiCapCalculation(taskId,type,resp);
        }catch (Exception e){
            String errormessage="建筑物抗震能力分布导出失败";
            log.error(errormessage,e);
            throw new RuntimeException(errormessage);
        }
    }


}
